var app = getApp();
var util = require("../../utils/util.js");
var siteinfo = require('../../siteinfo.js');
Page({
  data:{
    type: true,//type为true账号密码登录，为false手机验证码登录
    getSmsCodeBtnTxt: "获取验证码",
    getSmsCodeBtnColor: "#1A1B39",
    phoneNum:'',//手机号
    userName:'',//用户名
    password:''//密码
  },
  onLoad: function (options) {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 3];
    if (prevPage.route == "pages/proAdd/proAdd" || prevPage.route == "pages/signInformation/signInformation") {
      app.globalData.btn = true
    } else {
      app.globalData.btn = false
    }
  },
  //切换登录方式
  change:function(){
    let _this = this;
    _this.setData({
      type: !_this.data.type
    })
  },
  //手机验证码登录-获取手机号
  getPhoneNum: function (e) {
    var value = e.detail.value;
    this.setData({
      phoneNum: value
    });
  },
  //手机验证码登录-获取验证码
  getSmsCode: function () {
    let _this = this;
    let phoneNum = _this.data.phoneNum;
    let count = 60;
    if (_this.checkPhoneNum(phoneNum)) {
      app._post_form(siteinfo.sendSms, {
        mobile: phoneNum,
        wxId: siteinfo.wxId
      }, function (result) {
        app.showSuccess(result.msg);
        let si = setInterval(function () {
          if (count > 0) {
            count--;
            _this.setData({
              getSmsCodeBtnTxt: count + ' s',
              getSmsCodeBtnColor: "#61C69A",
              smsCodeDisabled: true
            });
          } else {
            _this.setData({
              getSmsCodeBtnTxt: "获取验证码",
              getSmsCodeBtnColor: "#666",
              smsCodeDisabled: false
            });
            count = 60;
            clearInterval(si);
          }
        }, 1000);
      })
     
    }
  },
  // 验证手机号正确性
  checkPhoneNum: function (param) {
    var phone = util.regexConfig().phone;
    var inputphoneNum = param.trim();
    if ("" == inputphoneNum) {
      app.showError('请输入手机号码');
      return false;
    }
    if (phone.test(inputphoneNum)) {
      return true;
    } else {
      app.showError('请输入正确的手机号码');
      return false;
    }
  },
  //密码登录-获取用户名
  getUserName: function (e) {
    var value = e.detail.value;
    this.setData({
      userName: value
    });
  },
   //密码登录-获取密码
  getPassword: function (e) {
    var value = e.detail.value;
    this.setData({
      passWord: value
    });
  },
  //手机验证码登录-获取密码
  getCode: function (e) {
    var value = e.detail.value;
    this.setData({
      code: value
    });
  },
  //type为password密码登录，为code验证码登录
  login: function (e) {
    var _this = this;
    var type = e.currentTarget.dataset.type
    var mobile = ''
    if (type=='password'){
      if ("" == _this.data.userName) {
        app.showError('请输入用户名或手机号');
        return false;
      }
      mobile = _this.data.userName
      if ("" == _this.data.passWord) {
        app.showError('请输入密码');
        return false;
      }
    }
    if (type == 'code') {
      if (_this.checkPhoneNum(_this.data.phoneNum)) {
        mobile = _this.data.phoneNum
      }
    }
    app._post_form(siteinfo.mobileLogin, {
      code: _this.data.code,
      account: mobile,
      passWord: _this.data.passWord,
      type: type,
      wxId: siteinfo.wxId
    }, function (result) {
      wx.setStorageSync('token', result.data.token);
      //是否含有openId
      wx.setStorageSync('isOpenId', result.data.isOpenId);
      wx.setStorageSync('userName', result.data.userName);
      wx.setStorageSync('headPortrait', result.data.headPortrait);
      app._getLogin(siteinfo.unreadNumApi, { type: 0 }, function (result) {
        var unreadNum = result.data
          wx.setStorageSync('unreadNum', unreadNum);
      });
      // 跳转回原页面
      _this.navigateBack();
    }, function (result) {
    }, function () {
    });
  },
  /**
  * 跳转回原页面
  */
  navigateBack: function () {
    // wx.navigateBack();
    wx.navigateBack({
      delta: 2
    })

  },
})